<template>
	<view class="points-mall">
		<view class="header">
			<view class="user-info">
				<image class="avatar" src="/static/logo.png" mode="aspectFill" />
				<view class="user-details">
					<text class="username">用户名</text>
					<text class="user-points">{{ userPoints }} 积分</text>
				</view>
			</view>
			<view class="search-bar">
				<input type="text" placeholder="搜索商品" />
				<text class="iconfont icon-search"></text>
			</view>
		</view>

		<scroll-view scroll-x class="category-scroll">
			<view class="category-item" v-for="(category, index) in categories" :key="index">
				{{ category }}
			</view>
		</scroll-view>

		<view class="product-list">
			<view v-for="(product, index) in products" :key="index" class="product-item">
				<image :src="product.image" mode="aspectFill" />
				<view class="product-info">
					<text class="product-name">{{ product.name }}</text>
					<view class="product-bottom">
						<view class="points-container">
							<text class="product-points">{{ product.points }}</text>
							<text class="points-label">积分</text>
						</view>
						<button @click="exchangeProduct(product)" :disabled="userPoints < product.points"
							:class="{ 'btn-disabled': userPoints < product.points }">兑换</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userPoints: 1000,
				categories: ['全部', '数码', '家居', '美妆', '服饰'],
				products: [{
						id: 1,
						name: '无线蓝牙耳机',
						points: 500,
						image: '/static/goods/3.webp'
					},
					{
						id: 2,
						name: '智能手环',
						points: 800,
						image: '/static/goods/2.webp'
					},
					{
						id: 3,
						name: '便携式充电宝',
						points: 300,
						image: '/static/goods/4.webp'
					},
					{
						id: 4,
						name: '香薰机',
						points: 400,
						image: '/static/goods/5.webp'
					},
				]
			}
		},
		methods: {
			exchangeProduct(product) {
				if (this.userPoints >= product.points) {
					uni.showModal({
						title: '兑换确认',
						content: `确定要兑换 ${product.name} 吗？将消耗 ${product.points} 积分`,
						success: (res) => {
							if (res.confirm) {
								this.userPoints -= product.points;
								uni.showToast({
									title: '兑换成功',
									icon: 'success'
								});
							}
						}
					});
				} else {
					uni.showToast({
						title: '积分不足',
						icon: 'none'
					});
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.points-mall {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;
	}

	.header {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}

	.user-info {
		display: flex;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.avatar {
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}

	.user-details {
		.username {
			font-size: 32rpx;
			font-weight: bold;
		}

		.user-points {
			font-size: 28rpx;
			color: #ff6600;
		}
	}

	.search-bar {
		position: relative;

		input {
			width: 100%;
			height: 70rpx;
			background-color: #f5f5f5;
			border-radius: 35rpx;
			padding: 0 60rpx 0 30rpx;
			font-size: 28rpx;
		}

		.icon-search {
			position: absolute;
			right: 30rpx;
			top: 50%;
			transform: translateY(-50%);
			color: #999;
		}
	}

	.category-scroll {
		white-space: nowrap;
		margin: 20rpx 0;

		.category-item {
			display: inline-block;
			padding: 10rpx 30rpx;
			margin-right: 20rpx;
			background-color: #fff;
			border-radius: 30rpx;
			font-size: 28rpx;
			color: #333;

			&:last-child {
				margin-right: 0;
			}
		}
	}

	.product-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}

	.product-item {
		width: 48%;
		margin-bottom: 20rpx;
		background-color: #fff;
		border-radius: 15rpx;
		overflow: hidden;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

		image {
			width: 100%;
			height: 250rpx;
		}

		.product-info {
			padding: 15rpx;
		}

		.product-name {
			font-size: 28rpx;
			margin-bottom: 10rpx;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
			height: 80rpx; // 固定高度，确保两行
		}

		.product-bottom {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 10rpx;
		}

		.points-container {
			display: flex;
			align-items: baseline;
		}

		.product-points {
			font-size: 32rpx;
			color: #ff6600;
			font-weight: bold;
		}

		.points-label {
			font-size: 24rpx;
			color: #999;
			margin-left: 4rpx;
		}

		button {
			background-color: #007aff;
			color: #fff;
			border: none;
			padding: 6rpx 20rpx;
			font-size: 24rpx;
			border-radius: 30rpx;
			min-width: 100rpx;
			text-align: center;

			&.btn-disabled {
				background-color: #ccc;
			}
		}
	}
</style>